package com.lr.wfxmanager.permission.service.impl;



import com.lr.wfxmanager.permission.bean.*;
import com.lr.wfxmanager.permission.mapper.UserMapper;
import com.lr.wfxmanager.permission.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class UserServiceImpl implements IUserService {
    @Resource
    private UserMapper userMapper;
    /*查询指定用户的权限树*/
    @Override
    public List<ParentFunctree> queryFunctree(String account) {
        return userMapper.queryFunctree(account);
    }

    /**
     * 角色权限树
     */
    public List<ParentFunctree> queryRolePermission(String roleCode){
        //查询当前角色的权限
        List<RoleGrant> hasPermission = userMapper.queryGrantByRoleCode(roleCode);
        //查询所有权限
        List<ParentFunctree> parentFunctrees = userMapper.queryAllGrant();
        //把当前角色拥有的权限给parentFunctrees的state做上标记
        if (null != hasPermission && !hasPermission.isEmpty()){//当前角色的权限不为空
            for (RoleGrant existP : hasPermission){//遍历当前拥有的权限
                String grantName = existP.getGrantName();//存在的权限
                Integer fid = existP.getFid();//存在的权限的id
                for (ParentFunctree allParentP : parentFunctrees){
                    //只有父权限被允许时,才可能会有子权限被允许
                    String pText = allParentP.getText();
                    if (pText.equals(grantName)){//判断有无此权限
                            //当前权限是允许的
                        allParentP.setFid(fid);
                        PermissionState state = new PermissionState();
                        state.setChecked(true);
                        allParentP.setState(state);
                        //再判断当前父权限的子权限是否被允许
                        List<ChildFunctree> nodes = allParentP.getNodes();
                        for (ChildFunctree allChildP : nodes){
                            for (RoleGrant existC : hasPermission){
                                if (allChildP.getText().equals(existC.getGrantName())){
                                    //子权限也有
                                    allChildP.setFid(existC.getFid());
                                    PermissionState state1 = new PermissionState();
                                    state1.setChecked(true);
                                    allChildP.setState(state1);
                                }
                            }
                        }
                    }
                }
            }
        }

        return parentFunctrees;
    }
    /*取消权限*/
    @Override
    public void cancelRoleGrant(Integer fid)throws Exception {
        userMapper.cancelRoleGrant(fid);
    }

    /**
     * 添加指定角色权限
     * @param moduleCode
     * @param roleCode
     */
    @Override
    public void addRoleGrant(String moduleCode, String roleCode)throws Exception {
        //给对应的父节点先添加
        userMapper.addRoleGrant(moduleCode.substring(0,6),roleCode);
        //添加勾选的子节点
        userMapper.addRoleGrant(moduleCode,roleCode);
    }
    /*取消指定角色父节点(包括子节点权限)*/
    @Override
    public void cancelRoleGrants(String moduleCode, String roleCode)throws Exception {
        userMapper.cancelRoleGrants(moduleCode,roleCode);
    }
    /*
    给父节点添加所有子节点权限
     */
    @Override
    public void addRoleGrants(String moduleCode, String roleCode)throws Exception {
       //给父节点添加
        userMapper.addRoleGrant(moduleCode,roleCode);
        //查询所有子节点权限id
        List<String> childs = userMapper.queryChildByParent(moduleCode);
        //给子节点添加授权
        for (String moduleId:childs) {
            userMapper.addRoleGrant(moduleId,roleCode);
        }
    }
    /*查询所有角色*/
    @Override
    public List<RoleInfo> queryAllRoleInfo() {
        return userMapper.queryAllRoleInfo();
    }


}
